<?php
namespace Modules\Frontend\Controllers;

class UserAddressController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function findFirstByIdAction($id) {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$service = $this -> account::getAddressServiceInstance() -> findFirstById($id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'user_address' => $service['user_address']]);
	}

	public function indexAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$service = $this -> account::getAddressServiceInstance() -> find([
			'conditions' => 'user_id = ' . $serviceUser['user'] -> user_id,
			'order' => 'is_default DESC, address_id DESC'
		], 999, 1);

		if($service['code'] != 0) {
			//return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'无地址数据,请添加']);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['user_address']]);
	}

	public function createAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$data = $this -> request -> getPost();
		unset($data['province']);
		unset($data['city']);
		unset($data['county']);
		if($data['is_default'] == 'true') {
			$findAddress = $this -> account::getAddressServiceInstance() -> find(['user_id'=>$serviceUser['user']->user_id]);
			if($findAddress['code'] == 0) {
				foreach($findAddress['user_address'] as $address) {
					if($address -> is_default == 0) {
						continue;
					}
					$address -> is_default = 0;
					$address -> save();
				}
			}
			$data['is_default'] = 1;
		} else {
			$data['is_default'] = 0;
		}
		$data['user_id'] = $serviceUser['user'] -> user_id;
		$data['create_time'] = time();

		$service = $this -> account::getAddressServiceInstance() -> create($data);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'创建成功']);
	}

	public function updateAction($id) {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$data = $this -> request -> getPut();
		unset($data['create_time']);
		unset($data['address_id']);
		unset($data['user_id']);
		unset($data['province']);
		unset($data['city']);
		unset($data['county']);
		if($data['is_default'] == 'true') {
			$findAddress = $this -> account::getAddressServiceInstance() -> find(['user_id'=>$serviceUser['user']->user_id]);
			if($findAddress['code'] == 0) {
				foreach($findAddress['user_address'] as $address) {
					if($address -> is_default == 0) {
						continue;
					}
					$address -> is_default = 0;
					$address -> save();
				}
			}
			$data['is_default'] = 1;
		} else {
			$data['is_default'] = 0;
		}

		$service = $this -> account::getAddressServiceInstance() -> update($id, $data);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'修改成功']);
	}

	public function deleteAction($id) {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$service = $this -> account::getAddressServiceInstance() -> delete($id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'删除成功']);
	}
}